package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.SkillOrderCommentDao;
import com.jbp.exception.RRException;
import com.jbp.model.Skill;
import com.jbp.model.SkillOrder;
import com.jbp.model.SkillOrderComment;
import com.jbp.model.User;
import com.jbp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import java.util.Map;

/**
 * 订单评论(SkillOrderComment)表服务实现类
 * @author cc
 * @since 2020-11-28 17:49:31
 */
@Service
public class SkillOrderCommentServiceImpl extends ServiceImpl<SkillOrderCommentDao, SkillOrderComment> implements SkillOrderCommentService {
    @Autowired
    private SkillOrderService skillOrderService;
    @Autowired
    private UserService userService;
    @Autowired
    private SkillService skillService;
    @Autowired
    private SkillUserService skillUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkillOrderComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("skill_user_id",params.get("skillUserId"));
        queryWrapper.orderByDesc("create_time");
        IPage<SkillOrderComment> page = this.page(
                new Query<SkillOrderComment>().getPage(params),
                queryWrapper
        );

        for (SkillOrderComment record : page.getRecords()) {
            User user = userService.getById(record.getCustomerId());
            record.setCustomerName(user.getName());
            record.setCustomerSex(user.getSex());
            record.setCustomerImage(user.getImg());

            Skill skill = skillService.getById(record.getSkillId());
            record.setSkillName(skill.getName());
            record.setSkillImg(skill.getImage());
        }

        return new PageUtils(page);
    }

    @Override
    public void save(SkillOrderComment req, Integer userId) {
        SkillOrder skillOrder = skillOrderService.getById(req.getSkillOrderId());
        if (skillOrder==null) {
            throw new RRException("订单不存在");
        }
        if (!userId.equals(skillOrder.getCustomerId())) {
            throw new RRException("不是本人订单");
        }
        if (skillOrder.getStatus().intValue()!=4) {
            throw new RRException("未完成订单");
        }
        SkillOrderComment one = getOne(new QueryWrapper<SkillOrderComment>()
                .eq("skill_order_id", req.getSkillOrderId())
                .eq("customer_id", skillOrder.getCustomerId())
        );
        if (one!=null) {
            throw new RRException("已经评论过了");
        }

        req.setCustomerId(skillOrder.getCustomerId());
        req.setGodId(skillOrder.getGodId());
        req.setSkillUserId(skillOrder.getSkillUserId());
        req.setSkillId(skillOrder.getSkillId());
        save(req);
    }

    @Override
    public PageUtils myComments(Map<String, Object> params, Integer userId) {
        QueryWrapper<SkillOrderComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.orderByDesc("create_time");
        IPage<SkillOrderComment> page = this.page(
                new Query<SkillOrderComment>().getPage(params),
                queryWrapper
        );

        for (SkillOrderComment record : page.getRecords()) {
            User user = userService.getById(record.getCustomerId());
            record.setCustomerName(user.getName());
            record.setCustomerSex(user.getSex());
            record.setCustomerImage(user.getImg());

            Skill skill = skillService.getById(record.getSkillId());
            record.setSkillName(skill.getName());
            record.setSkillImg(skill.getImage());
            record.setSkillPrice(skillUserService.getPrice(record.getSkillUserId()));
        }

        return new PageUtils(page);
    }

    @Override
    public SkillOrderComment myComment(Integer skillOrderId, Integer userId) {

        return getOne(new QueryWrapper<SkillOrderComment>()
                .eq("skill_order_id", skillOrderId)
                .eq("customer_id", userId)
        );
    }
}